﻿var _oArea;

//功能：编辑页面中点击左侧导航，刷新右侧明细
//说明：原理是创建一个iframe对象，打开明细页面
//参数：sArea		-- 明细对象的id
//		sUrl		-- 要打开页面的url地址
//		xmlParams	-- 后缀参数用xml描述，如下：
//		<params>
//			<param name="oId" value="crmForm.CustGUID.value">
//		</params>
function loadArea(sArea, sUrl, xmlParams) {

    // Set currently selected area
    if (!_oArea) _oArea = tdAreas.firstChild;

    var o = document.all(sArea);

    // Display the requested area - If it doesn't exist, create one
    if (o) {
        if (o == _oArea && event.srcElement._reload != "1") return false;

        if (event.srcElement._reload == "1")		// 标记要求重刷新
        {
            if (event.srcElement.parentElement.selectedIndex == 0)		// 刷窗口页面
                window.location.reload();
            else		// 刷导航标签对应页面
            {
                oIframe = document.all(sArea + "Frame");
                //oIframe.src = oIframe.src;
                oIframe.contentWindow.location.reload();
            }
        }

        _oArea.style.display = "none";
        o.style.display = "inline";
    }
    else {
        if (!sUrl || sUrl == "") sUrl = "/ErrPage.aspx";

        // 拼写参数字符串		
        var sParam = "";

        if (xmlParams != undefined && xmlParams != "") {
            var xmlDom = new ActiveXObject("Microsoft.XMLDOM");

            xmlDom.loadXML(xmlParams);

            var paramNodes = xmlDom.documentElement.selectNodes("//params/param")

            for (var i = 0; i < paramNodes.length; i++) {
                if (sParam != "")
                    sParam += "&";

                try {
                    sParam += paramNodes[i].attributes.getNamedItem("name").value + "=" + escape(eval(paramNodes[i].attributes.getNamedItem("value").value));
                }
                catch (e) { }
            }
        }

        // 给URL附加后最参数
        if (sParam != "") {
            if (sUrl.indexOf("?") == -1)
                sUrl = sUrl + "?" + sParam;
            else
                sUrl = sUrl + "&" + sParam;
        }

        // 拼写HTML
        // 注意：如果生成规则发生改变，需要同步修改 AppNavBar 控件的后端代码
        html = "<iframe src='" + sUrl + "' id='" + sArea + "Frame' scrolling='yes' isArea='1' width='100%' height='100%' frameborder='0'></iframe>";

        o = document.createElement("DIV");
        o.id = sArea
        o.style.overflow = "auto";
        o.style.height = window.screen.height - 200;
        
        o.innerHTML = html;

        _oArea.style.display = "none";

        tdAreas.appendChild(o);
    }

    event.srcElement._reload = "0"; 	// 清除标记

    _oArea = o;
}


// sLocation - fax/phone number/letter addres depending on the activity form
//可能是自动发送传真、电话、E-mail用，暂时没有发现在什么地方用到？
function addActivityToSingle(iType, spId, spType, spName, sPartyId, sPartyType, sPartyName, sPartyCode, sLocation) {
    var sPartyIds = new Array(sPartyId);
    var sPartyTypes = new Array(sPartyType.toString());
    var sPartyNames = new Array(sPartyName);
    var sPartyCodes = new Array(sPartyCode.toString());

    addActivityTo(iType, spId, spType, spName, sPartyIds, sPartyTypes, sPartyNames, sPartyCodes, sLocation);
}

function escapeAmp(str) {
    return encodeURIComponent(str);
}

function addActivityTo(iType, spId, spType, spName, sPartyIds, sPartyTypes, sPartyNames, sPartyCodes, sLocation) {
    var iX, iY;
    var sUrl = "?";

    if (spId != null && spType != null) {
        // test for optional params
        sUrl += "&pId=" + spId;
        sUrl += "&pType=" + spType;
        sUrl += "&pName=" + escapeAmp(spName);
    }

    // tack on multiple parties
    if (sPartyIds != undefined) {
        var len = sPartyIds.length;

        for (var i = 0; i < len; i++) {
            sUrl += "&partyid=" + sPartyIds[i];
            sUrl += "&partytype=" + sPartyTypes[i];
            sUrl += "&partyname=" + escapeAmp(sPartyNames[i]);
            sUrl += "&partycode=" + sPartyCodes[i];
            sUrl += "&partyaddressused=";  // Needed for email; not used when prepopulating.
            sUrl += "&contactInfo=" + escapeAmp(sLocation[i]);
        }
    }

    openFrmObj(sUrl, "", iType);
}
